/**
 * ActionQueue
 * @Chunning.Xu
 * 2011 Nov 23, 2011 4:00:07 PM
 **/
package com.lux.product.controllers.queue
{
	import flash.events.EventDispatcher;
	
	[Event(name="process_complete", type="com.lux.product.controllers.queue.QueueEvents")]
	[Event(name="start_process", type="com.lux.product.controllers.queue.QueueEvents")]

	public class ActionQueue extends EventDispatcher
	{
		//START.
		
		private var queue:Vector.<QueueItem>;
		
		public var idle:Boolean;

		public function ActionQueue()
		{
			queue = new Vector.<QueueItem>;
			idle = true;
		}
		
		public function push(item:QueueItem):void
		{
			if(!idle)
			{
				queue.push(item);
				return;
			}
			
			idle = false;
			dispatchEvent(new QueueEvents(QueueEvents.START_PROCESS));
			item.method.apply(item.self,item.params);
		}
		
		public function processNext():void
		{
			if(queue.length == 0)
			{
				idle = true;
				dispatchEvent(new QueueEvents(QueueEvents.PROCESS_COMPLETE));
				return;
			}
			
			var item:QueueItem = queue.shift();
			item.method.apply(item.self,item.params);
		}
		
		public function clear():void
		{
			queue.splice(0,queue.length);
		}
		
		//END
	}
}